package cn.tedu;

import java.net.CookieHandler;
import java.sql.*;
import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名");
        String username=sc.nextLine();
        System.out.println("请输入密码");
        String password=sc.nextLine();
        try (Connection conn=DBUtils.getConn()){
//*********************************************************************************************************************//
//            Statement s= conn.createStatement();                                                                     //
//            String sql="select count(*) from user where username='"+username+"'and password='"+password+"'";         //
//            //执行查询语句                                                                                             //
//            ResultSet rs=s.executeQuery(sql);                                                                        //
//*********************************************************************************************************************//

            String sql="select count(*) from user where username=? and password=?";
            //预编译的SQL执行对象
            PreparedStatement ps=conn.prepareStatement(sql);
            //替换掉SQL语句中的?
            ps.setString(1,username);
            ps.setString(2,password);
            //执行SQL语句
            ResultSet rs=ps.executeQuery();
            //游标往下移动
            rs.next();
            //获取ResultSet中查询出的唯一一条数据
            int count=rs.getInt(1);
            if (count>0){
                System.out.println("登入成功");
            }else System.out.println("登入失败");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
